home *** CD-ROM | disk | FTP | other *** search
- import java.awt.Color;
- import java.awt.Graphics;
-
- class Cell {
- public static final int VALUE = 0;
- public static final int LABEL = 1;
- public static final int URL = 2;
- public static final int FORMULA = 3;
- Node parseRoot;
- boolean needRedisplay;
- boolean selected = false;
- boolean transientValue = false;
- public int type;
- String valueString = "";
- String printString = "v";
- float value;
- Color bgColor;
- Color fgColor;
- Color highlightColor;
- int width;
- int height;
- SpreadSheet app;
- CellUpdater updaterThread;
- boolean paused = false;
-
- public Cell(SpreadSheet app, Color bgColor, Color fgColor, Color highlightColor, int width, int height) {
- this.app = app;
- this.bgColor = bgColor;
- this.fgColor = fgColor;
- this.highlightColor = highlightColor;
- this.width = width;
- this.height = height;
- this.needRedisplay = true;
- }
-
- public void setRawValue(float f) {
- this.valueString = Float.toString(f);
- this.value = f;
- }
-
- public void setValue(float f) {
- this.setRawValue(f);
- this.printString = "v" + this.valueString;
- this.type = 0;
- this.paused = false;
- this.app.recalculate();
- this.needRedisplay = true;
- }
-
- public void setTransientValue(float f) {
- this.transientValue = true;
- this.value = f;
- this.needRedisplay = true;
- this.app.recalculate();
- }
-
- public void setUnparsedValue(String s) {
- switch (s.charAt(0)) {
- case 'v':
- this.setValue(0, s.substring(1));
- return;
- case 'u':
- this.setValue(2, s.substring(1));
- return;
- case 'l':
- this.setValue(1, s.substring(1));
- return;
- case 'f':
- this.setValue(3, s.substring(1));
- return;
- default:
- }
- }
-
- public String parseFormula(String formula, Node node) {
- formula.length();
- if (formula == null) {
- return null;
- } else {
- String subformula = this.parseValue(formula, node);
- if (subformula != null && subformula.length() != 0) {
- if (subformula == formula) {
- return formula;
- } else {
- char op;
- switch (op = subformula.charAt(0)) {
- case '/':
- case '-':
- case '+':
- case '*':
- String restFormula = subformula.substring(1);
- Node right;
- subformula = this.parseValue(restFormula, right = new Node());
- if (subformula != restFormula) {
- Node left = new Node(node);
- node.left = left;
- node.right = right;
- node.op = op;
- node.type = 0;
- return subformula;
- }
-
- return formula;
- case ')':
- return subformula;
- case '\u0000':
- return null;
- default:
- return formula;
- }
- }
- } else {
- return null;
- }
- }
- }
-
- public String parseValue(String formula, Node node) {
- char c = formula.charAt(0);
- String restFormula = formula;
- if (c == '(') {
- restFormula = formula.substring(1);
- String subformula = this.parseFormula(restFormula, node);
- if (subformula == null || subformula.length() == restFormula.length()) {
- return formula;
- }
-
- if (subformula.charAt(0) != ')') {
- return formula;
- }
-
- restFormula = subformula;
- } else {
- if (c >= '0' && c <= '9') {
- float var6;
- try {
- var6 = Float.valueOf(formula);
- } catch (NumberFormatException var10) {
- return formula;
- }
-
- int i;
- for(i = 0; i < formula.length(); ++i) {
- c = formula.charAt(i);
- if ((c < '0' || c > '9') && c != '.') {
- break;
- }
- }
-
- node.type = 1;
- node.value = var6;
- restFormula = formula.substring(i);
- return restFormula;
- }
-
- if (c >= 'A' && c <= 'Z') {
- int column = c - 65;
- restFormula = formula.substring(1);
- int row = Float.valueOf(restFormula).intValue();
-
- int i;
- for(i = 0; i < restFormula.length(); ++i) {
- c = restFormula.charAt(i);
- if (c < '0' || c > '9') {
- break;
- }
- }
-
- node.row = row - 1;
- node.column = column;
- node.type = 2;
- if (i == restFormula.length()) {
- restFormula = null;
- } else {
- restFormula = restFormula.substring(i);
- if (restFormula.charAt(0) == 0) {
- return null;
- }
- }
- }
- }
-
- return restFormula;
- }
-
- public void setValue(int type, String s) {
- this.paused = false;
- if (this.type == 2) {
- this.updaterThread.stop();
- this.updaterThread = null;
- }
-
- this.valueString = new String(s);
- this.type = type;
- this.needRedisplay = true;
- switch (type) {
- case 0:
- this.setValue(Float.valueOf(s));
- break;
- case 1:
- this.printString = "l" + this.valueString;
- break;
- case 2:
- this.printString = "u" + this.valueString;
- this.updaterThread = new CellUpdater(this);
- this.updaterThread.start();
- break;
- case 3:
- this.parseFormula(this.valueString, this.parseRoot = new Node());
- this.printString = "f" + this.valueString;
- }
-
- this.app.recalculate();
- }
-
- public String getValueString() {
- return this.valueString;
- }
-
- public String getPrintString() {
- return this.printString;
- }
-
- public void select() {
- this.selected = true;
- this.paused = true;
- }
-
- public void deselect() {
- this.selected = false;
- this.paused = false;
- this.needRedisplay = true;
- this.app.repaint();
- }
-
- public void paint(Graphics g, int x, int y) {
- if (this.selected) {
- g.setColor(this.highlightColor);
- } else {
- g.setColor(this.bgColor);
- }
-
- g.fillRect(x, y, this.width - 1, this.height);
- if (this.valueString != null) {
- switch (this.type) {
- case 0:
- case 1:
- g.setColor(this.fgColor);
- break;
- case 2:
- g.setColor(Color.blue);
- break;
- case 3:
- g.setColor(Color.red);
- }
-
- if (this.transientValue) {
- g.drawString("" + this.value, x, y + this.height / 2 + 5);
- } else if (this.valueString.length() > 14) {
- g.drawString(this.valueString.substring(0, 14), x, y + this.height / 2 + 5);
- } else {
- g.drawString(this.valueString, x, y + this.height / 2 + 5);
- }
- }
-
- this.needRedisplay = false;
- }
- }
-